package com.example.guidepage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class GuidePageAdapter extends PagerAdapter {

    private List<View> views;

    private int[] images = {R.drawable.a, R.drawable.b, R.drawable.c};

    private String[] titles = {"1", "2", "3"};

    private String[] desc = {"萨拉如果客户觉得是的身份和", "的实力开发工具和的斯洛伐克国家和速度放缓", "阿萨的人看过韩剧立刻就会的风格离开对方是个机会"};

    public GuidePageAdapter(Context context, View.OnClickListener clickStartListener) {
        // 初始化views
        views = new ArrayList<>(images.length);
        for (int i = 0; i < images.length; i++) {
            View view = LayoutInflater.from(context).inflate(R.layout.guide_page, null, false);

            ImageView imageView = view.findViewById(R.id.imageView);
            imageView.setImageResource(images[i]);

            TextView textViewTitle = view.findViewById(R.id.textView_title);
            textViewTitle.setText(titles[i]);

            TextView textViewDesc = view.findViewById(R.id.textView_desc);
            textViewDesc.setText(desc[i]);

            Button buttonStart = view.findViewById(R.id.button_start);
            if (i != titles.length - 1) {
                buttonStart.setVisibility(View.GONE);
            } else {
                buttonStart.setOnClickListener(clickStartListener);
            }

            views.add(view);
        }
    }

    /**
     * 获取页面的个数
     * @return
     */
    @Override
    public int getCount() {
        return views.size();
    }

    /**
     * 判断当前视图是否来自指定对象
     * @param view
     * @param object
     * @return
     */
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    /**
     * 实例化指定位置的页面，并添加到容器
     * @param container
     * @param position
     * @return
     */
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        container.addView(views.get(position));
        return views.get(position);
    }

    /**
     * 从容器中销毁指定位置的页面
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(views.get(position));
    }
}
